﻿@using DCMS.ViewModel.Models.Configuration;
@using DCMS.Web.Extensions;
@using DCMS.Web.Framework.UI;
@using DCMS.Web.Framework;



@model PricingStructureListModel


<section id="content_wrapper">

    <!-- Start: Topbar-Dropdown -->
    <div id="topbar-dropmenu">
        <div id="topbar-dropmenu">
            @await Html.PartialAsync("_ToolBox")
        </div>
    </div>
    <!-- End: Topbar-Dropdown -->
    <!-- Start: Topbar -->
    <header id="topbar">
        <div class="topbar-left">
            <ol class="breadcrumb">
                <li class="crumb-active">
                    <a href="@Url.RouteUrl("HomePage")">首页</a>
                </li>
                <li class="crumb-icon">
                    <a href="dashboard.html">
                        <span class="glyphicon glyphicon-home"></span>
                    </a>
                </li>
                <li class="crumb-link">
                    <a href="#">设置</a>
                </li>
                <li class="crumb-trail">价格体系设置</li>
            </ol>
        </div>
        <div class="topbar-right">
            <div class="ml15 ib va-m" id="toggle_sidemenu_r">
                <a href="#" class="pl5">
                    <i class="fa fa-sign-in fs22 text-primary"></i>
                    <span class="badge badge-hero badge-danger">3</span>
                </a>
            </div>
        </div>
    </header>
    <!-- End: Topbar -->
    <!-- Begin: Content -->
    <section id="content" class="formSection">

        <div class="special-alerts">
            <div class="alert alert-danger light alert-dismissable" id="alert-demo-2" style="display: none;">
                <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
                <i class="fa fa-info pr10"></i>
                <strong>您没有被授权此操作!</strong>
            </div>
        </div>

        <div class="panel">
            <div class="panel-heading">
                <div class="panel-title">
                    <span class="glyphicon glyphicon-tasks"></span> 体系配置
                </div>
            </div>
            <div class="panel-body p10 pt10">
                <div class="tab-block">
                    <ul class="nav nav-tabs tabs-bg">
                        <li class="active">
                            <a href="#tab10_1" data-toggle="tab" aria-expanded="true"><i class="fa fa-align-justify text-purple"></i> 按客户类别指定</a>
                        </li>
                        <li>
                            <a href="#tab10_2" data-toggle="tab" aria-expanded="true"><i class="fa fa-align-justify text-purple"></i> 按客户指定</a>
                        </li>
                    </ul>
                    <div class="tab-content">
                        <div id="tab10_1" class="tab-pane active">

                            <table class="table table-striped table-bordered table-hover" id="PricingStructures1"></table>

                        </div>
                        <div id="tab10_2" class="tab-pane">
                            <table class="table table-striped table-bordered table-hover" id="PricingStructures2"></table>
                        </div>
                    </div>
                </div>
            </div>
            <div class="panel-footer">
                <button id="FormSubmit" type="button" class="btn btn-primary" value="保存"><i class="fa fa-floppy-o mr5"></i>保存</button>
            </div>
        </div>



    </section>
    <!-- End: Content -->
</section>


@await Html.PartialAsync("_Windows")



@{
//Html.AddCssFileParts(ResourceLocation.Head, "~/assets/skin/default_skin/css/theme.css");
//Html.AddCssFileParts(ResourceLocation.Head, "~/Scripts/Toastr/toastr.css");
//Html.AddCssFileParts(ResourceLocation.Head, "~/Scripts/Tables/bootstrap-table.css");

//Html.AddCssFileParts(ResourceLocation.Head, "~/vendor/plugins/datepicker/css/bootstrap-datetimepicker.css");
//Html.AddCssFileParts(ResourceLocation.Head, "~/vendor/plugins/daterange/daterangepicker.css");
//Html.AddCssFileParts(ResourceLocation.Head, "~/vendor/plugins/tagmanager/tagmanager.css");

//Html.AddCssFileParts(ResourceLocation.Head, "~/vendor/plugins/fancytree/skin-win8/ui.fancytree.min.css");
//Html.AddCssFileParts(ResourceLocation.Head, "~/Content/zTree.theme.metro.min.css");
}

@section NewDefinFrameWorkScripts
{
    @{
        Html.AddScriptParts(ResourceLocation.Footer, "~/vendor/jquery/jquery-1.11.1.min.js", order: 999);
        Html.AddScriptParts(ResourceLocation.Footer, "~/vendor/jquery/jquery_ui/jquery-ui.min.js", order: 999);
        Html.AddScriptParts(ResourceLocation.Footer, "~/assets/js/bootstrap/bootstrap.min.js", order: 999);
    }
}

@section CurPageScripts
{
    @{
//Html.AddScriptParts(ResourceLocation.Footer, "~/assets/js/utility/jquery.sparkline.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/jquery.validate.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/jquery.validate.unobtrusive.js");
////Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/Tables/jquery.treegrid.min.js");

//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/Tables/bootstrap-table.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/Tables/locale/bootstrap-table-zh-CN.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/Tables/bootstrap-table-treeview.js");


//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/globalize.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/moment.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/moment-with-locales.js");

////Html.AddScriptParts(ResourceLocation.Footer, "~/vendor/plugins/daterange/daterangepicker.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/vendor/plugins/datepicker/js/bootstrap-datetimepicker.js");

//Html.AddScriptParts(ResourceLocation.Footer, "~/vendor/plugins/jquerymask/jquery.maskedinput.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/vendor/plugins/tagmanager/tagmanager.js");

//Html.AddScriptParts(ResourceLocation.Footer, "~/vendor/plugins/fancytree/jquery.fancytree-all.min.js");

//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/jquery.ztree.all-3.5.min.js");

//Html.AddScriptParts(ResourceLocation.Footer, "~/assets/js/bootstrap/holder.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/Toastr/Toastr.min.js");
//Html.AddScriptParts(ResourceLocation.Footer, "~/Scripts/Custom/Common.js");
    }
    <script type="text/javascript">
        jQuery(document).ready(function () {
            "use strict";
            // Init Theme Core
            Core.init({
                sbm: "sb-l-c",
            });
            // Init Demo JS
            Demo.init();

			//权限提示
			if ($(this).getUrlQueryString("Authorize") == "noAuthorize") {
				$('#alert-demo-2').fadeToggle();
				setTimeout(function () {
					$('#alert-demo-2').fadeToggle();
				}, 3000);
			}

            var ChannelDatas = @Html.Raw(Model.ChannelDatas);
            var LevelDatas = @Html.Raw(Model.LevelDatas);
            var TierPricePlanDatas = @Html.Raw(Model.TierPricePlanDatas);

             //
            $("#PricingStructures1").bootstrapTable({
            @Html.Raw(Model.Items1.Count > 0 ? "url: '/Setting/AsyncPricingStructures?type=0'," : "length: 9,")
                striped: true,
            cache: false,
            pagination: true,
            showPagination: false,
            sidePagination: "server",
            sortable: false,
            sortOrder: "asc",
            search: false,
            strictSearch: false,
            showColumns: false,
            showRefresh: false,
            clickToSelect: false,
            uniqueId: "id",
            showToggle: false,
            editable: true,
            cardView: false,
                detailView: false,
                showFooter: false,
            onLoadSuccess: function (data) {
                $(".fixed-table-pagination").hide();
            },
            columns: [
                {
                    title: '<i class="fa fa-gear fs18"></i>',
                    align: 'center',
                    width: '50',
                    formatter: function (value, row, index) {
                        return m_pagerow + index + 1;
                    }
                },
                { field: 'CustomerId', visible: false },
                { field: 'ChannelId', visible: false },
                { field: 'DistrictIds', visible: false },
                { field: 'EndPointLevel', visible: false },
                { field: 'PreferredPrice', visible: false },
                { field: 'SecondaryPrice', visible: false },
                { field: 'FinalPrice', visible: false },
                {
                    field: 'ChannelName',
                    title: '渠道',
                    align: 'center',
                    width: '200'
                },
                {
                    field: 'DistrictName',
                    title: '片区',
                    align: 'center',
                    width: '200'
                },
                {
                    field: 'EndPointLevelName',
                    title: '等级',
                    align: 'center',
                    width: '200'
                },
                {
                    field: 'PreferredPriceName',
                    title: '首选价格',
                    align: 'center',
                     width: '200'

                },
                {
                    field: 'SecondaryPriceName',
                    title: '次选价格',
                    align: 'center',
                    width: '200'
                },
                {
                    field: 'FinalPriceName',
                    title: '末选价格',
                    align: 'center',
                    width: '200'
                },
                {
                    field: 'Operate',
                    title: '操作',
                    align: 'center',
                    width: '100',
                    events: {
                        'click .rowAppend': function (e, value, row, index) {// 添加
                            $('#PricingStructures1').bootstrapTable('insert', row);
                        },
                        'click .rowDel': function (e, value, row, index) {// 删除
                            var datas = $('#PricingStructures1').bootstrapTable('getData');
							var len = datas.length;
							if (len == 1) {
								$(this).showToastr('数据不能删除');
								return;
							}
							if (row.uniqueid == undefined) {
                                $("#PricingStructures1").bootstrapTable('remove', { field: "id", values: [parseInt(row.Id)] });
							}
							else {
                                $("#PricingStructures1").bootstrapTable('remove', { field: "uniqueid", values: [parseInt(row.uniqueid)] });
							}
                        }
                    },
                    formatter: function (value, row, index) {
                        return [
                            '<button type="button" class="btn btn-sm btn-default rowAppend" title="添加"><i class="fa fa-plus" ></i></button>',
                            '<button type="button" class="btn btn-sm btn-default rowDel" title="删除"><i class="fa fa-trash-o"></i></button>'
                        ].join('');
                    }
                }
                ],
                onClickCell: function (field, value, row, $element) {

					if (field == "Operate") {
						return false;
					}

                    var input = $($element).find("input");
                    var select = $($element).find("select");
                    if (input.length > 0 || select.length > 0)
                        return false;
                    //当前列所在行的索引
                    var index = $element.parent().attr("data-index");

                    if (field == "ChannelName") {
                        var html = '<select name="' + field + '" id="' + field + "_" + row.Id + '" class="form-control">';
                        $.each(ChannelDatas, function (index, data) {
                            //console.log(data);
                            html += '<option value="' + data.Id + '"  ' + (row.ChannelId == data.Id ? "selected" : "") + '>' + data.Name + '</option>';
                        });
                        html += '</select>';
                        $($element).html(html);
                        var inputObj = $("#" + field + "_" + row.Id + "");
                        inputObj.focus();
						inputObj.select();

                        inputObj.bind("focusout", function () {
                            $("#PricingStructures1").bootstrapTable('updateRow', { index: index, row: { ChannelId: $(this).val(), ChannelName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });

                        inputObj.bind("change", function () {
                            $("#PricingStructures1").bootstrapTable('updateRow', { index: index, row: { ChannelId: $(this).val(), ChannelName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });
                    }
                    else if (field == "DistrictName")
                    {
                        var html = '<div class="input-group">' +
                            '<input type="text" class="form-control input-sm"  name="DistrictName" value="' + (typeof (value) == 'undefined' ? "" : value) + '" id="' + field + "_" + row.Id + '">' +
                            '<span class="input-group-btn"><button class="btn btn-default btn-sm searchdistricts" id="search_' + field + "_" + row.Id + '" data-index="' + index + '"><span class="glyphicon glyphicon-search"></span></button></span>' +
                            '</div>';
                        $($element).html(html);
                        var inputObj = $("#search_" + field + "_" + row.Id);
                        inputObj.focus();
                        inputObj.select();
                        //选择
                        inputObj.bind("click", function ()
                        {
                            //console.log("searchdistricts==");
                            var actionUrl = "/Terminal/AsyncDistrictsSelectPopup?type=all&style=checkbox";
                            $(this).districtSearchSelectPopup($("#DistrictsModalWindow"), actionUrl, null, "选择片区", $("#DistrictsForm"), function (data) {
                                var ids = [];
                                var names = [];
                                $.each(data, function (i, dist) {
                                    ids.push(dist.id);
                                    names.push(dist.name);
                                });
                                row.DistrictIds = ids.join(",")
                                row.DistrictName = names.join(",")
                                $($element).html("" + names.join(",") + "");
                                $("#PricingStructures1").bootstrapTable('updateRow', { index: index, row: { DistrictIds: ids.join(","), DistrictName: names.join(",") } });
                            });
                        });

                    }
                    else if (field == "EndPointLevelName")
                    {
                        var html = '<select name="' + field + '" id="' + field + "_" + row.Id + '" class="form-control">';
                        $.each(LevelDatas, function (index, data) {
                            html += '<option value="' + data.Id + '"  ' + (row.EndPointLevel == data.Id ? "selected" : "") + '>' + data.Name + '</option>';
                        });
                        html += '</select>';
                        $($element).html(html);
                        var inputObj = $("#" + field + "_" + row.Id + "");
                        inputObj.focus();
						inputObj.select();

                        inputObj.bind("focusout", function () {
                            $("#PricingStructures1").bootstrapTable('updateRow', { index: index, row: { EndPointLevel: $(this).val(), EndPointLevelName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });

                        inputObj.bind("change", function () {
                            $("#PricingStructures1").bootstrapTable('updateRow', { index: index, row: { EndPointLevel: $(this).val(), EndPointLevelName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });
                    }
                    else if (field == "PreferredPriceName") {
                        var html = '<select name="' + field + '" id="' + field + "_" + row.Id + '" class="form-control">';
                        $.each(TierPricePlanDatas, function (index, data) {
                            //console.log(data);
							//html += '<option value="' + data.PricesPlanId + '_' + data.PriceTypeId + '">' + data.Name + '</option>';
                            html += '<option value="' + data.PricesPlanId + '_' + data.PriceTypeId + '"  ' + (row.PreferredPrice == (data.PricesPlanId + '_' + data.PriceTypeId) ? "selected" : "") + '>' + data.Name + '</option>';
                        });
                        html += '</select>';
                        $($element).html(html);
                        var inputObj = $("#" + field + "_" + row.Id + "");
                        inputObj.focus();
						inputObj.select();

                        inputObj.bind("focusout", function () {
                            $("#PricingStructures1").bootstrapTable('updateRow', { index: index, row: { PreferredPrice: $(this).val(), PreferredPriceName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });

                        inputObj.bind("change", function () {
                            $("#PricingStructures1").bootstrapTable('updateRow', { index: index, row: { PreferredPrice: $(this).val(), PreferredPriceName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });
                    }
                    else if (field == "SecondaryPriceName") {
                        var html = '<select name="' + field + '" id="' + field + "_" + row.Id + '" class="form-control">';
                        $.each(TierPricePlanDatas, function (index, data) {
                            //console.log(data);
							//html += '<option value="' + data.PricesPlanId + '_' + data.PriceTypeId + '">' + data.Name + '</option>';
                            html += '<option value="' + data.PricesPlanId + '_' + data.PriceTypeId + '"  ' + (row.SecondaryPrice == (data.PricesPlanId + '_' + data.PriceTypeId) ? "selected" : "") + '>' + data.Name + '</option>';
                        });
                        html += '</select>';
                        $($element).html(html);
                        var inputObj = $("#" + field + "_" + row.Id + "");
                        inputObj.focus();
						inputObj.select();

                        inputObj.bind("focusout", function () {
                            $("#PricingStructures1").bootstrapTable('updateRow', { index: index, row: { SecondaryPrice: $(this).val(), SecondaryPriceName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });

                        inputObj.bind("change", function () {
                            $("#PricingStructures1").bootstrapTable('updateRow', { index: index, row: { SecondaryPrice: $(this).val(), SecondaryPriceName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });
                    } else if (field == "FinalPriceName") {
                        var html = '<select name="' + field + '" id="' + field + "_" + row.Id + '" class="form-control">';
                        $.each(TierPricePlanDatas, function (index, data) {
                            //console.log(data);
							//html += '<option value="' + data.PricesPlanId + '_' + data.PriceTypeId + '">' + data.Name + '</option>';
                            html += '<option value="' + data.PricesPlanId + '_' + data.PriceTypeId + '"  ' + (row.FinalPrice == (data.PricesPlanId + '_' + data.PriceTypeId) ? "selected" : "") + '>' + data.Name + '</option>';
                        });
                        html += '</select>';
                        $($element).html(html);
                        var inputObj = $("#" + field + "_" + row.Id + "");
                        inputObj.focus();
						inputObj.select();

                        inputObj.bind("focusout", function () {
                            $("#PricingStructures1").bootstrapTable('updateRow', { index: index, row: { FinalPrice: $(this).val(), FinalPriceName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });

                        inputObj.bind("change", function () {
                            $("#PricingStructures1").bootstrapTable('updateRow', { index: index, row: { FinalPrice: $(this).val(), FinalPriceName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });
                    }

                    $.each($('#PricingStructures1').bootstrapTable('getData'), function (i, row) {
                        //console.log(row);
                    });
                }
        });



            $("#PricingStructures2").bootstrapTable({
            @Html.Raw(Model.Items2.Count > 0 ? "url: '/Setting/AsyncPricingStructures?type=1'," : "length: 9,")
                striped: true,
            cache: false,
            pagination: true,
            showPagination: false,
            sidePagination: "server",
            sortable: false,
            sortOrder: "asc",
            search: false,
            strictSearch: false,
            showColumns: false,
            showRefresh: false,
            clickToSelect: false,
            uniqueId: "id",
            showToggle: false,
            editable: true,
            cardView: false,
                detailView: false,
                showFooter: false,
            onLoadSuccess: function (data) {
                $(".fixed-table-pagination").hide();
            },
            columns: [
                {
                    title: '<i class="fa fa-gear fs18"></i>',
                    align: 'center',
                    width: '50',
                    formatter: function (value, row, index) {
                        return m_pagerow + index + 1;
                    }
                },
                { field: 'CustomerId', visible: false },
                { field: 'PreferredPrice', visible: false },
                { field: 'SecondaryPrice', visible: false },
                { field: 'FinalPrice', visible: false },
                {
                    field: 'CustomerName',
                    title: '客户',
                    align: 'center',
                    width: '200'
                },
                {
                    field: 'PreferredPriceName',
                    title: '首选价格',
                    align: 'center',
                     width: '200'

                },
                {
                    field: 'SecondaryPriceName',
                    title: '次选价格',
                    align: 'center',
                    width: '200'
                },
                {
                    field: 'FinalPriceName',
                    title: '末选价格',
                    align: 'center',
                    width: '200'
                },
                {
                    field: 'Operate',
                    title: '操作',
                    align: 'center',
                    width: '100',
                    events: {
                        'click .rowAppend': function (e, value, row, index) {// 添加
                            $('#PricingStructures2').bootstrapTable('insert', row);
                        },
                        'click .rowDel': function (e, value, row, index) {// 删除
                            var datas = $('#PricingStructures2').bootstrapTable('getData');
							var len = datas.length;
							if (len == 1) {
								$(this).showToastr('数据不能删除');
								return;
							}
							if (row.uniqueid == undefined) {
                                $("#PricingStructures2").bootstrapTable('remove', { field: "id", values: [parseInt(row.Id)] });
							}
							else {
                                $("#PricingStructures2").bootstrapTable('remove', { field: "uniqueid", values: [parseInt(row.uniqueid)] });
							}
                        }
                    },
                    formatter: function (value, row, index) {
                        return [
                            '<button type="button" class="btn btn-sm btn-default rowAppend" title="添加"><i class="fa fa-plus" ></i></button>',
                            '<button type="button" class="btn btn-sm btn-default rowDel" title="删除"><i class="fa fa-trash-o"></i></button>'
                        ].join('');
                    }
                }
                ],
                onClickCell: function (field, value, row, $element) {

					if (field == "Operate") {
						return false;
					}

                    var input = $($element).find("input");
                    var select = $($element).find("select");
                    if (input.length > 0 || select.length > 0)
                        return false;
                    //当前列所在行的索引
                    var index = $element.parent().attr("data-index");

                    if (field == "CustomerName")
                    {
                        var html = '<div class="input-group">' +
                            '<input type="text" class="form-control input-sm"  name="CustomerName" value="' + (typeof (value) == 'undefined' ? "" : value) + '" id="' + field + "_" + row.Id + '">' +
                            '<span class="input-group-btn"><button class="btn btn-default btn-sm searchproduct" id="search_' + field + "_" + row.Id + '" data-index="' + index + '"><span class="glyphicon glyphicon-search"></span></button></span>' +
                            '</div>';

                        $($element).html(html);
                        var inputObj = $("#search_" + field + "_" + row.Id);
                        inputObj.focus();
                        inputObj.select();
                        inputObj.bind("focusout", function () {
                            $("#CostExpenditureItems").bootstrapTable('updateRow', { index: index, row: { CustomerName: $(this).val() } });
                            $($element).html("" + value + "");
                        });
                        //选择终端
                        inputObj.bind("click", function () {
                            var selectIndex = $(this).attr("data-index");
                            var actionUrl = "/Terminal/AsyncSearchSelectPopup";
                            $(this).terminalSearchSelectPopup($("#TerminalSelectModalWindow"), actionUrl, { Index: selectIndex }, "选择终端", $("#TerminalSelectForm"), "popterminal_datatable", function (rows)
                            {
                                //console.log(rows);
                                //$("#PricingStructures2").bootstrapTable('updateRow', {
                                //    index: selectIndex, row: { CustomerId: rows.id, CustomerName: rows.name }
                                //});
                            });
                            return false;
                        });
                    }
                    else if (field == "PreferredPriceName") {
                        var html = '<select name="' + field + '" id="' + field + "_" + row.Id + '" class="form-control">';
                        $.each(TierPricePlanDatas, function (index, data) {
                            //console.log(data);
							//html += '<option value="' + data.PricesPlanId + '_' + data.PriceTypeId + '">' + data.Name + '</option>';
                            html += '<option value="' + data.PricesPlanId + '_' + data.PriceTypeId + '"  ' + (row.PreferredPrice == (data.PricesPlanId + '_' + data.PriceTypeId) ? "selected" : "") + '>' + data.Name + '</option>';
                        });
                        html += '</select>';
                        $($element).html(html);
                        var inputObj = $("#" + field + "_" + row.Id + "");
                        inputObj.focus();
						inputObj.select();

                        inputObj.bind("focusout", function () {
                            $("#PricingStructures2").bootstrapTable('updateRow', { index: index, row: { PreferredPrice: $(this).val(), PreferredPriceName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });

                        inputObj.bind("change", function () {
                            $("#PricingStructures2").bootstrapTable('updateRow', { index: index, row: { PreferredPrice: $(this).val(), PreferredPriceName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });
                    }
                    else if (field == "SecondaryPriceName") {
                        var html = '<select name="' + field + '" id="' + field + "_" + row.Id + '" class="form-control">';
                        $.each(TierPricePlanDatas, function (index, data) {
                            //console.log(data);
							//html += '<option value="' + data.PricesPlanId + '_' + data.PriceTypeId + '">' + data.Name + '</option>';
                            html += '<option value="' + data.PricesPlanId + '_' + data.PriceTypeId + '"  ' + (row.SecondaryPrice == (data.PricesPlanId + '_' + data.PriceTypeId) ? "selected" : "") + '>' + data.Name + '</option>';
                        });
                        html += '</select>';
                        $($element).html(html);
                        var inputObj = $("#" + field + "_" + row.Id + "");
                        inputObj.focus();
						inputObj.select();

                        inputObj.bind("focusout", function () {
                            $("#PricingStructures2").bootstrapTable('updateRow', { index: index, row: { SecondaryPrice: $(this).val(), SecondaryPriceName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });

                        inputObj.bind("change", function () {
                            $("#PricingStructures2").bootstrapTable('updateRow', { index: index, row: { SecondaryPrice: $(this).val(), SecondaryPriceName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });
                    } else if (field == "FinalPriceName") {
                        var html = '<select name="' + field + '" id="' + field + "_" + row.Id + '" class="form-control">';
                        $.each(TierPricePlanDatas, function (index, data) {
                            //console.log(data);
							//html += '<option value="' + data.PricesPlanId + '_' + data.PriceTypeId + '">' + data.Name + '</option>';
                            html += '<option value="' + data.PricesPlanId + '_' + data.PriceTypeId + '"  ' + (row.FinalPrice == (data.PricesPlanId + '_' + data.PriceTypeId) ? "selected" : "") + '>' + data.Name + '</option>';
                        });
                        html += '</select>';
                        $($element).html(html);
                        var inputObj = $("#" + field + "_" + row.Id + "");
                        inputObj.focus();
						inputObj.select();

                        inputObj.bind("focusout", function () {
                            $("#PricingStructures2").bootstrapTable('updateRow', { index: index, row: { FinalPrice: $(this).val(), FinalPriceName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });

                        inputObj.bind("change", function () {
                            $("#PricingStructures2").bootstrapTable('updateRow', { index: index, row: { FinalPrice: $(this).val(), FinalPriceName: inputObj.find("option:selected").text() } });
                            $($element).html("" + inputObj.find("option:selected").text() + "");
                        });
                    }

                    $.each($('#PricingStructures2').bootstrapTable('getData'), function (i, row) {
                        //console.log(row);
                    });
                }
            });



                   //保存服务器
            var submitRemoteServer = function (callback)
            {

                var pricingStructures = [];
                var pricingStructures1 = $('#PricingStructures1').bootstrapTable('getData');
                var pricingStructures2 = $('#PricingStructures2').bootstrapTable('getData');
                $.each(pricingStructures1, function (i, row)
				{
                    if (row.ChannelId != "" || row.DistrictIds != "" || row.EndPointLevel != "") {
						pricingStructures.push({
							Id: row.Id,
                            PriceType: 0,
                            CustomerId: 0,
                            CustomerName: 0,
                            ChannelId: row.ChannelId == "" ? 0 : row.ChannelId,
                            DistrictIds: row.DistrictIds,
                            DistrictName: row.DistrictName,
                            EndPointLevel: row.EndPointLevel == "" ? 0 : row.EndPointLevel,
                            PreferredPrice: row.PreferredPrice,
                            SecondaryPrice: row.SecondaryPrice,
                            FinalPrice: row.FinalPrice,
                            Order: 0
                        });
                    }

                });

                $.each(pricingStructures2, function (i, row)
                {
                    if (row.CustomerId != "" || row.CustomerName != "") {
						pricingStructures.push({
                            Id: row.Id,
                            PriceType: 1,
                            CustomerId: row.CustomerId == "" ? 0 : row.CustomerId,
                            CustomerName: row.CustomerName,
                            ChannelId: 0,
                            DistrictIds: 0,
                            DistrictName: row.DistrictName,
                            EndPointLevel: 0,
                            PreferredPrice: row.PreferredPrice,
                            SecondaryPrice: row.SecondaryPrice,
                            FinalPrice: row.FinalPrice,
                            Order: 0
                        });
                    }

                });

                //保存的数据
                var postData = {
                    Datas: pricingStructures
                };
                //debug
                //console.log(postData);
                //return false;

                //保存服务器
                $(this).remoteSubmit("post", "/Setting/PricingStructure", postData, null, callback);

            };

            //保存单据项目
            $("#FormSubmit").click(function () {
                submitRemoteServer(function (data) {
                    if (data.Success) {
                         window.location.href = "/Setting/PricingStructure";
                    }
                });
            });

            //客户
			window.operateEventsPopTerminal = {
				'click .rowSelect': function (e, value, row, index) {
					try {
						var selectIndex = $(this).attr("data-id");

						$("#PricingStructures2").bootstrapTable('updateRow', {
							index: selectIndex, row: { CustomerId: row.Id, CustomerName: row.Name }
						});
						$('#TerminalSelectModalWindow').modal('hide');
					}
					catch (err) {
						console.log(err);
						$(this).showToastr(err);
						return;
					}
				}
			};




        });

    </script>
}
